package com.xydtech.ecard.merchant.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.merchant.domain.EcardBranchMerchantMarket;
import com.xydtech.ecard.merchant.service.IEcardBranchMerchantMarketService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商户网格化营销统计Controller
 *
 * @author yuzhiguo
 * @date 2024-10-17 上午9:02
 */
@Controller
@RequestMapping("/merchant/branchMerchantMarket")
public class EcardBranchMerchantMarketController extends BaseController {

    @Autowired
    private IEcardBranchMerchantMarketService ecardBranchMerchantMarketService;

    private String prefix = "merchant";

    @RequiresPermissions("merchant:branchMerchantMarket:view")
    @GetMapping()
    public String merchant()
    {
        return prefix + "/branchMerchantMarket";
    }

    @RequiresPermissions("merchant:branchMerchantMarket:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardBranchMerchantMarket ecardBranchMerchantMarket) {
        String dateSearch = ecardBranchMerchantMarket.getDateSearch();//时间条件
        //时间条件为空，传当前日期
        if (StringUtils.objIsNullOrEmpty(dateSearch)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
            String currentDate = sdf.format(new Date());
            ecardBranchMerchantMarket.setDateSearch(currentDate);//当前日期
            ecardBranchMerchantMarket.setLastWeekDay(DateUtils.getLastWeekDay(currentDate));//获取上一个工作日
        } else {
            ecardBranchMerchantMarket.setDateSearch(dateSearch);//当前日期
            ecardBranchMerchantMarket.setLastWeekDay(DateUtils.getLastWeekDay(dateSearch));//获取上一个工作日
        }
        startPage();
        List<EcardBranchMerchantMarket> list = ecardBranchMerchantMarketService.selectBranchMerchantMarketInfo(ecardBranchMerchantMarket);
        return getDataTable(list);
    }

    @RequiresPermissions("merchant:branchMerchantMarket:export")
    @Log(title = "导出商户网格化营销统计信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardBranchMerchantMarket ecardBranchMerchantMarket)
    {
        String dateSearch = ecardBranchMerchantMarket.getDateSearch();//时间条件
        //时间条件为空，传当前日期
        if (StringUtils.objIsNullOrEmpty(dateSearch)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
            String currentDate = sdf.format(new Date());
            ecardBranchMerchantMarket.setDateSearch(currentDate);//当前日期
            ecardBranchMerchantMarket.setLastWeekDay(DateUtils.getLastWeekDay(currentDate));//获取上一个工作日
        } else {
            ecardBranchMerchantMarket.setDateSearch(dateSearch);//当前日期
            ecardBranchMerchantMarket.setLastWeekDay(DateUtils.getLastWeekDay(dateSearch));//获取上一个工作日
        }
        List<EcardBranchMerchantMarket> list = ecardBranchMerchantMarketService.selectBranchMerchantMarketInfo(ecardBranchMerchantMarket);
        ExcelUtil<EcardBranchMerchantMarket> util = new ExcelUtil<EcardBranchMerchantMarket>(EcardBranchMerchantMarket.class);
        return util.exportExcel(list, "商户网格化营销统计信息");
    }
}
